package des.wangku.operate.standard.testing;

import java.io.File;
import java.net.URLEncoder;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import des.wangku.operate.standard.utls.UtilsFile;
import des.wangku.operate.standard.utls.UtilsJsoup;
import des.wangku.operate.standard.utls.UtilsJsoupCase;
import des.wangku.operate.standard.utls.UtilsJsoupExt;
import des.wangku.operate.standard.utls.UtilsJsoupLink;
import des.wangku.operate.standard.utls.UtilsPathFile;
import static des.wangku.operate.standard.utls.UtilsConsts.ACC_ENTER;

public class M99114comgongying {

	public static void main2(String[] args) {
		String ACC_Path = "G:/下载文档夹/m.99114.com/";
		List<String> list=UtilsPathFile.getFileList(ACC_Path, "txt");
		File file = new File("G:/下载文档夹/m.99114.com.txt");
		for(String e:list) {
			System.out.println("e:"+e);
			File f = new File(e);
			StringBuilder sb=UtilsFile.readFile(f);
			UtilsFile.writeFile(file, sb.toString()+ACC_ENTER);
		}
	}
	public static void main(String[] args) {
		String ACC_Path = "G:/下载文档夹/m.99114.com/";
		String url = "http://m.99114.com/category/find";
		List<Element> list = UtilsJsoup.getElementAll(url, "section");
		int bigmin = 4, bigmax = list.size()-1;
		try {
			for (int i = bigmin; i <= bigmax; i++) {
				Element main = list.get(i);
				String bigcategory = UtilsJsoupCase.getTextFirstByClass(main, "classifyTitle");
				String bigcategorypath = ACC_Path + bigcategory + "/";
				File filebig = new File(bigcategorypath);
				if (!filebig.exists()) filebig.mkdir();
				
				Elements es = main.select("li");
				int len=es.size();
				for (int ii = 0; ii < len; ii++) {
					Element e = es.get(ii);
					String smallcategory = e.text();
					String head="["+i+"-" + ii + "/"+(len-1)+"]:" + bigcategory + "\t" + smallcategory;
					String smallcategorypath = bigcategorypath + "/" + smallcategory + ".txt";
					File filesmall = new File(smallcategorypath);
					if (filesmall.exists()) continue;
					String lines="http://m.99114.com/gongying/"+smallcategory+"/";
					UtilsFile.writeFile(smallcategorypath, "栏目\t"+lines);
					String encode = URLEncoder.encode(smallcategory + "", "UTF-8");
					int p = 0;
					while (p < 200) {
						url = "http://m.99114.com/gongying/" + encode + "_-_-_" + p + "_-_-.html";
						//System.out.println(url);
						Document doc = UtilsJsoupExt.getDoc(url);//,UtilsJsoup.MODE_URL);
						if (doc.text().length() < 100) 	break;
						System.out.println(head+"\t" + p+"页\t"+url);
						Elements gs = doc.getElementsByClass("firstLine");
						for (Element gse : gs) {
							String line = "";
							String name = UtilsJsoupLink.getTextFirst(gse);
							String href = UtilsJsoupLink.getHrefFirst(gse);
							line = ACC_ENTER + "" + name + "\t" + href;
							UtilsFile.writeFile(smallcategorypath, line);
						}
						p++;
					}
					UtilsFile.writeFileHeaderSmall(smallcategorypath, "最多"+p+"页\t"+url+ACC_ENTER);

				}

			}
		} catch (Exception e) {
			System.out.println("error:" + url);
			e.printStackTrace();
			System.out.println("error:" + e.toString());
		}

		/*
		 * String[] arr = { "ab|2", "|2", "|", "div|7" };
		 * for (String e : arr) {
		 * int index = e.indexOf('|');
		 * System.out.println(e + ":" + index);
		 * String v = e.substring(0, index);
		 * String val = e.substring(index + 1, e.length());
		 * System.out.println(e + "=" + v + ":\t:" + val);
		 * }
		 */
	}
}
